package com.simpleim.starter.command;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * 客户端命令
 */
@Service
public class MenuCommand implements BaseCommand{
    private static final Logger LOG = LoggerFactory.getLogger(MenuCommand.class);

    public static final String KEY = "0";

    private String allCommandsShow;

    private String commandInput;

    @Override
    public void exec(Scanner scanner) {
        LOG.info("请输入某个操作指令：{}", allCommandsShow);
        commandInput = scanner.next();
    }

    @Override
    public String type() {
        return KEY;
    }

    @Override
    public String title() {
        return "show 所有命令";
    }

    public String getCommandInput() {
        return commandInput;
    }

    public void setAllCommand(Map<String, BaseCommand> commandMap) {
        Set<Map.Entry<String, BaseCommand>> entrys =
                commandMap.entrySet();
        Iterator<Map.Entry<String, BaseCommand>> iterator =
                entrys.iterator();

        StringBuilder menus = new StringBuilder();
        menus.append("[menu] ");
        while (iterator.hasNext()) {
            BaseCommand next = iterator.next().getValue();

            menus.append(next.type())
                    .append("->")
                    .append(next.title())
                    .append(" | ");

        }
        allCommandsShow = menus.toString();
    }
}
